/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.user.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.user.entity.CampusinfoGroup;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;

/**
 * 用户信息Controller
 * @author 陈维乾
 * @version 2018-03-31
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfo/user")
public class CampusinfoUserController extends BaseController {

	@Autowired
	private CampusinfoUserService campusinfoUserService;
	
	//学校
	@Autowired
	private CampusinfoSchoolService campusinfoSchoolService;
	
	//
	@Autowired
	private CampusinfoUserGroupService campusinfoUserGroupService;
	
	//组别
	@Autowired
	private CampusinfoGroupService campusinfoGroupService;
	
	
	@ModelAttribute
	public User get(@RequestParam(required=false) String id) {
		User entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoUserService.get(id);
		}
		if (entity == null){
			entity = new User();
		}
		return entity;
	}
	
	@RequiresPermissions("user:ampusinfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {

		Page<User> page = campusinfoUserService.findUserGroupPage(new Page<User>(request, response), user); 
		model.addAttribute("page", page);
		
		return "modules/user/userList";
	}

	@RequiresPermissions("user:ampusinfo:view")
	@RequestMapping(value = "form")
	public String form(User user, Model model) {
		
		//TODO
		//将campusinfoGroups集合传给view
		List<CampusinfoGroup> campusinfoGroups = campusinfoGroupService.findAllList(new CampusinfoGroup());
		model.addAttribute("campusinfoGroups", campusinfoGroups);
		
		//TODO
		//将campusinfoSchool集合传给view
		List<CampusinfoSchool>  campusinfoSchools = campusinfoSchoolService.findAllList(new CampusinfoSchool());
		model.addAttribute("campusinfoSchools", campusinfoSchools);

		model.addAttribute("user", user);
		return "modules/user/userForm";
	}

	@RequiresPermissions("user:ampusinfo:edit")
	@RequestMapping(value = "save")
	public String save(User user, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, user)){
			return form(user, model);
		}
		campusinfoUserService.saveUserAndUserGroup(user);
		//TODO???????????????????????
		//插入user 同事向campusinfo_user_group表中插入id,groupid,userid  
		
		addMessage(redirectAttributes, "保存用户信息成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfo/user/?repage";
	}
	
	@RequiresPermissions("user:ampusinfo:edit")
	@RequestMapping(value = "delete")
	public String delete(User user, RedirectAttributes redirectAttributes) {
		campusinfoUserService.delete(user);
		addMessage(redirectAttributes, "删除用户信息成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfo/user/?repage";
	}

}